﻿using System.Collections.Generic;

namespace Tomato.Allure
{
	internal class ProjectsImpl : Tomato.Allure.IProjects
	{
		private EnvDTE.Projects m_instance;

		public int Count
		{
			get { return m_instance.Count; }
		}

		public Tomato.Allure.IProject this[ int index ]
		{
			get 
			{
				// Index for Item() starts with 1.
				EnvDTE.Project project = m_instance.Item( index + 1 );

				if( project != null )
				{
					return new ProjectImpl( project );
				}

				return null; 
			}
		}

		public Tomato.Allure.IProject this[ string name ]
		{
			get
			{
				EnvDTE.Project project = m_instance.Item( name );

				if( project != null )
				{
					return new ProjectImpl( project );
				}

				return null;
			}
		}

		public ProjectsImpl( EnvDTE.Projects instance )
		{
			m_instance = instance;
		}

		public IEnumerator<Tomato.Allure.IProject> GetEnumerator()
		{
			for( int i = 1 ; i <= m_instance.Count ; ++i )
			{
				EnvDTE.Project project = m_instance.Item( i );
				if( project != null )
				{
					yield return new ProjectImpl( project );
				}
				else
				{
					yield return null;
				}
			}
		}

		System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
		{
			return GetEnumerator();
		}
	}
}
